home *** CD-ROM | disk | FTP | other *** search
/ The Java 3D API Specification (2nd Edition) / The Java 3D API Specification (2nd Edition).iso / programs / examples / Sound / MoveAppBoundingLeaf.java < prev    next >
Text File  |  2000-04-28  |  15KB  |  362 lines

  1. /*
  2.  *    @(#)MoveAppBoundingLeaf.java 1.10 00/03/31 14:29:38
  3.  *
  4.  * Copyright (c) 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. /*
  32.  * This Java3D program:
  33.  *    Creates an instance of the JavaSoundMixer AudioDevice, initializing it
  34.  *         and attaching it to the PhysicalEnvironment with a special version
  35.  *         of UniverseBuilder (UniverseBuildJS).
  36.  *    Creates one Point sound sources.
  37.  *    Creates two Soundscapes with their own aural attributes.  Each
  38.  *         of these Soundscapes defines its own bounding leaf region.
  39.  *    Creates and executes a custom behavior (AudioBehaviorMoveOne) that
  40.  *         starts sound playing and transforms the AuralAttribute positions
  41.  *         by modifying the TransformGroup that contains both soundscape
  42.  *         nodes.
  43.  * Usage: java MoveAppBoundingLeaf [ URLpath [ name ]]
  44.  *
  45.  * The first optional command line parameter is the URL path to directory
  46.  *    containing:
  47.  *       (1) the files that will be named on the command line, or
  48.  *       (2) the javaone/data/sounds directory that contains the default files
  49.  *    If not given the default path is:
  50.  *       file:/net/java3d/export/java3d/javaone/data/sounds
  51.  *    NOTE: This default path is only valid on Solaris Eng Menlo Park network.
  52.  *    A path must be supplied if sound files to be used are in a different
  53.  *    directory location.
  54.  *    NOTE: When running on Java3D Menlo Park Test WinTel platform path should
  55.  *    be set to
  56.  *       file:/java3d/export/java3d/javaone/data/sounds
  57.  *
  58.  * The second thru fourth optional command line parameters are sound file names
  59.  *    If not given the default file name is:
  60.  *       unicycle_close.au
  61.  */
  62.  
  63. import java.applet.Applet;
  64. import java.net.URL;
  65. import java.awt.*;
  66. import java.awt.event.*;
  67. import com.sun.j3d.utils.applet.MainFrame;
  68. import com.sun.j3d.utils.geometry.ColorCube;
  69. import com.sun.j3d.utils.geometry.Sphere;
  70. import com.sun.j3d.utils.universe.*;
  71. import java.io.File;
  72. import javax.media.j3d.*;
  73. import javax.vecmath.*;
  74.  
  75. public class MoveAppBoundingLeaf extends Applet {
  76.  
  77.     // File name of sound sample
  78.     private static  int filenamesGiven = 0;
  79.     private static URL[] url = new URL[1];
  80.     private static String[] filename = new String[1];
  81.     private static String path = null;
  82.     private static boolean filenamesSet = false;
  83.  
  84.     public BranchGroup createSceneGraph() {
  85.     // Create the root of the subgraph
  86.     BranchGroup objRoot = new BranchGroup();
  87.  
  88.     // Create the transform group node and initialize it to the identity.
  89.     // Enable the TRANSFORM_WRITE capability so that our behavior code
  90.     // can modify it at runtime.  Add it to the root of the subgraph.
  91.     TransformGroup objTrans = new TransformGroup();
  92.     objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  93.     objRoot.addChild(objTrans);
  94.  
  95.     // Create a simple shape leaf node and add it into the scene graph.
  96.     objTrans.addChild(new ColorCube(0.7));
  97.  
  98.     // Create a new Behavior object that will perform the desired
  99.     // operation on the specified transform object and add it into the
  100.     // scene graph.
  101.     Transform3D yAxis = new Transform3D();
  102.         Alpha rotation = new Alpha(-1, Alpha.INCREASING_ENABLE,
  103.                                    0, 0,
  104.                                    20000, 0, 0,
  105.                                    0, 0, 0);
  106.         RotationInterpolator rotator =
  107.             new RotationInterpolator(rotation,
  108.                                      objTrans, yAxis,
  109.                                      0.0f, (float) Math.PI*2.0f);
  110.     BoundingSphere bounds =
  111.         new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  112.     rotator.setSchedulingBounds(bounds);
  113.     objTrans.addChild(rotator);
  114.         //
  115.         // Create a sound node and add it to the scene graph
  116.         //
  117.         PointSound sound = new PointSound();
  118.         sound.setCapability(PointSound.ALLOW_ENABLE_WRITE);
  119.         sound.setCapability(PointSound.ALLOW_INITIAL_GAIN_WRITE);
  120.         sound.setCapability(PointSound.ALLOW_SOUND_DATA_WRITE);
  121.         sound.setCapability(PointSound.ALLOW_SCHEDULING_BOUNDS_WRITE);
  122.         sound.setCapability(PointSound.ALLOW_CONT_PLAY_WRITE);
  123.         sound.setCapability(PointSound.ALLOW_RELEASE_WRITE);
  124.         sound.setCapability(PointSound.ALLOW_DURATION_READ);
  125.         sound.setCapability(PointSound.ALLOW_IS_PLAYING_READ);
  126.         sound.setCapability(PointSound.ALLOW_POSITION_WRITE);
  127.         sound.setCapability(PointSound.ALLOW_LOOP_WRITE);
  128.  
  129.     BoundingSphere soundBounds =
  130.         new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  131.         sound.setSchedulingBounds(soundBounds);
  132.     objTrans.addChild(sound);
  133.  
  134.         /*
  135.          * Spheres denoting aural attribute regions
  136.          */
  137.         TransformGroup objTransChild1 = new TransformGroup();
  138.         TransformGroup objTransChild2 = new TransformGroup();
  139.         objTransChild1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  140.         objTransChild2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  141.         Transform3D translate1 = new Transform3D();
  142.         Transform3D translate2 = new Transform3D();
  143.         Vector3f vector1 = new Vector3f( 2.0f, 0.0f, 0.0f);
  144.         Vector3f vector2 = new Vector3f(-2.0f, 0.0f, 0.0f);
  145.         translate1.setTranslation(vector1);
  146.         translate2.setTranslation(vector2);
  147.         objTransChild1.setTransform(translate1);
  148.         objTransChild2.setTransform(translate2);
  149.         Sphere sphere1 = new Sphere(0.42f);
  150.         Sphere sphere2 = new Sphere(0.38f);
  151.  
  152.         Appearance app = new Appearance();
  153.         app.setCapability( Appearance.ALLOW_POLYGON_ATTRIBUTES_WRITE );
  154.         sphere1.setAppearance( app );
  155.         sphere2.setAppearance( app );
  156.         objTransChild1.addChild(sphere1);
  157.         objTransChild2.addChild(sphere2);
  158.  
  159.         objTrans.addChild(objTransChild1);
  160.         objTrans.addChild(objTransChild2);
  161.         /**
  162.          * Define Soundscapes/AuralAttributes
  163.          */
  164.         // First Aural Attributes
  165.         Point2f[] distanceFilter = new Point2f[2];
  166.         distanceFilter[0] = new Point2f(6.0f, 6000f);
  167.         distanceFilter[1] = new Point2f(17.0f, 700f);
  168.         AuralAttributes attributes = new AuralAttributes(1.0f, 2.0f, 0.3f,
  169.                4.0f, 5, distanceFilter, 0.8f, 0.0f);
  170.         attributes.setReverbDelay(90.0f) ;
  171.         attributes.setReflectionCoefficient(0.999f) ;
  172.         attributes.setReverbOrder(9) ;
  173.  
  174.         // SoundScape
  175.     BoundingSphere bounds1 =
  176.         new BoundingSphere(new Point3d(2.0,0.0,0.0), 3.5);
  177.  
  178.         BoundingLeaf   sScapeBounds1 = new BoundingLeaf(bounds1);
  179.         Soundscape soundScape = new Soundscape(null, attributes);
  180.         soundScape.setApplicationBoundingLeaf(sScapeBounds1);
  181.         /** addChild of BoundingLeaf as well**/
  182.     objTrans.addChild(sScapeBounds1);
  183.  
  184.         AuralAttributes queryAttribs = new AuralAttributes();
  185.         if (queryAttribs == null)
  186.             System.out.println(" new AuralAttributes returned NULL");
  187.         // else
  188.         //  System.out.println(" new AuralAttributes returned " + queryAttribs);
  189.         queryAttribs = soundScape.getAuralAttributes();
  190.         if (queryAttribs == null)
  191.             System.out.println("getAuralAttributes returned NULL");
  192.         // else
  193.         //     System.out.println("AuralAttributes for Soundscape 1:");
  194.         float tmpFloat = queryAttribs.getAttributeGain();
  195.         // System.out.println("    Gain = " + tmpFloat);
  196.         tmpFloat = queryAttribs.getRolloff();
  197.         // System.out.println("    Rolloff = " + tmpFloat);
  198.         tmpFloat = queryAttribs.getReflectionCoefficient();
  199.         // System.out.println("    Reflection Coeff = " + tmpFloat);
  200.         tmpFloat = queryAttribs.getReverbDelay();
  201.         // System.out.println("    Delay = " + tmpFloat);
  202.         int tmpInt = queryAttribs.getReverbOrder();
  203.         // System.out.println("    Order = " + tmpInt);
  204.         int length = queryAttribs.getDistanceFilterLength();
  205.         // System.out.println("    Filter length = " + length);
  206.         Point2f[] tmpPoint = new Point2f[length];
  207.         tmpPoint = new Point2f[length];
  208.         for (int i=0; i< length; i++) 
  209.             tmpPoint[i] = new Point2f();
  210.         queryAttribs.getDistanceFilter(tmpPoint);
  211.         // for (int i=0; i< length; i++) 
  212.         //     System.out.println("    Distance Filter = (" + tmpPoint[i].x +
  213.         //         ", " + tmpPoint[i].y + ")" );
  214.         tmpFloat = queryAttribs.getFrequencyScaleFactor();
  215.         // System.out.println("    Freq scalefactor = " + tmpFloat);
  216.         tmpFloat = queryAttribs.getVelocityScaleFactor();
  217.         // System.out.println("    Velocity scalefactor= " + tmpFloat);
  218.     objTrans.addChild(soundScape);
  219.  
  220.         // System.out.println("SoundScape2**********************************");
  221.         Soundscape soundScape2 = new Soundscape();
  222.         distanceFilter = new Point2f[2];
  223.         distanceFilter[0] = new Point2f(2.0f, 20000.0f);
  224.         distanceFilter[1] = new Point2f(20.0f, 2000.0f);
  225.         AuralAttributes attributes2 = new AuralAttributes();
  226.         attributes2.setAttributeGain(1.2f);
  227.         attributes2.setRolloff(2.2f);
  228.         attributes2.setReverbDelay(1313.0f) ;
  229.         attributes2.setReflectionCoefficient(1.0f) ;
  230.         attributes2.setReverbOrder(15) ;
  231.         distanceFilter[0] = new Point2f(5.0f, 15000.0f);
  232.         distanceFilter[1] = new Point2f(15.0f, 500.0f);
  233.         attributes2.setDistanceFilter(distanceFilter);
  234.         attributes2.setFrequencyScaleFactor(0.8f);
  235.         attributes2.setVelocityScaleFactor(0.0f);
  236.     BoundingSphere bounds2 =
  237.         new BoundingSphere(new Point3d(-2.0,0.0,0.0), 0.38);
  238.         BoundingLeaf   sScapeBounds2 = new BoundingLeaf(bounds2);
  239.         soundScape2.setApplicationBoundingLeaf(sScapeBounds2);
  240.         // set BoundingLeaf as a child of transform node 
  241.     objTrans.addChild(sScapeBounds2);
  242.         soundScape2.setAuralAttributes(attributes2);
  243.  
  244.         queryAttribs = soundScape2.getAuralAttributes();
  245.         if (queryAttribs == null)
  246.             System.out.println(" new AuralAttributes returned NULL");
  247.         // else
  248.         //  System.out.println(" new AuralAttributes returned " + queryAttribs);
  249.         // System.out.println("AuralAttributes for Soundscape 2:");
  250.         tmpFloat = queryAttribs.getAttributeGain();
  251.         // System.out.println("    Gain = " + tmpFloat);
  252.         tmpFloat = queryAttribs.getRolloff();
  253.         // System.out.println("    Rolloff = " + tmpFloat);
  254.         tmpFloat = queryAttribs.getReflectionCoefficient();
  255.         // System.out.println("    Reflection Coeff = " + tmpFloat);
  256.         tmpFloat = queryAttribs.getReverbDelay();
  257.         // System.out.println("    Delay = " + tmpFloat);
  258.         tmpInt = queryAttribs.getReverbOrder();
  259.         // System.out.println("    Order = " + tmpInt);
  260.         length = queryAttribs.getDistanceFilterLength();
  261.         // System.out.println("    Filter length = " + length);
  262.         tmpPoint = new Point2f[length];
  263.         for (int i=0; i< length; i++) 
  264.             tmpPoint[i] = new Point2f();
  265.         queryAttribs.getDistanceFilter(tmpPoint);
  266.         // for (int i=0; i< length; i++) 
  267.         //     System.out.println("    Distance Filter = (" + tmpPoint[i].x +
  268.         //           ", " + tmpPoint[i].y + ")" );
  269.         tmpFloat = queryAttribs.getFrequencyScaleFactor();
  270.         // System.out.println("    Freq scalefactor = " + tmpFloat);
  271.         tmpFloat = queryAttribs.getVelocityScaleFactor();
  272.         // System.out.println("    Velocity scalefactor= " + tmpFloat);
  273.     objTrans.addChild(soundScape2);
  274.  
  275.         //
  276.     // Create a new Behavior object that will play the sound
  277.     //
  278.         AudioBehaviorMoveOne player = new AudioBehaviorMoveOne(sound, 
  279.                 filename[0]);
  280.     player.setSchedulingBounds(soundBounds);
  281.     objTrans.addChild(player);
  282.  
  283.     return objRoot;
  284.     }
  285.  
  286.     public MoveAppBoundingLeaf() {
  287.     }
  288.  
  289.     public void init() {
  290.         if (!filenamesSet) {
  291.             // path is null if started from applet
  292.         if (path == null) {
  293.             path = getCodeBase().toString();
  294.         }
  295.     
  296.             /*
  297.              * append given file name to given URL path
  298.              */
  299.             if (filenamesGiven > 0) {
  300.                 filename[0] = new String(path + "/" + filename[0]);
  301.             }
  302.             else {
  303.                 // fill in default file names if all three not given
  304.                 filename[0] = new String(path + "/techno_machine.au");
  305.             }
  306.             filenamesSet = true;
  307.         }
  308.  
  309.     setLayout(new BorderLayout());
  310.         GraphicsConfiguration config =
  311.            SimpleUniverse.getPreferredConfiguration();
  312.  
  313.         Canvas3D c = new Canvas3D(config);
  314.     add("Center", c);
  315.  
  316.         /*
  317.          * Change filenames into URLs
  318.          */
  319.         String substr = filename[0].substring(0,4);
  320.         try {
  321.             url[0] = new URL(filename[0]);
  322.         }
  323.         catch (Exception e) {
  324.                 return;
  325.         }
  326.         /*
  327.          * Create a simple scene and attach it to the virtual universe
  328.          */
  329.         SimpleUniverse u = new SimpleUniverse(c);
  330.         AudioDevice audioDev = u.getViewer().createAudioDevice();
  331.         BranchGroup scene = createSceneGraph();
  332.  
  333.         // This will move the ViewPlatform back a bit so the
  334.         // objects in the scene can be viewed.
  335.         u.getViewingPlatform().setNominalViewingTransform();
  336.  
  337.     u.addBranchGraph(scene);
  338.     }
  339.  
  340.     //
  341.     // The following allows AuralAttributes to be run as an application
  342.     // as well as an applet
  343.     //
  344.     public static void main(String[] args) {
  345.         if (args.length > 0) {
  346.         path = "file:" + args[0];
  347.     }
  348.     else {
  349.       path = "file:.";
  350.     }
  351.  
  352.     if (args.length > 1) {
  353.         filename[0] = args[1];
  354.         if (filename[0] != null) {
  355.             filenamesGiven++ ;
  356.         }
  357.     }
  358.  
  359.     new MainFrame(new MoveAppBoundingLeaf(), 256, 256);
  360.     }
  361. }
  362.